import 'dart:convert';

import 'package:crypto/crypto.dart';

class StringUtil {
  static String calculateMD5(String input) {
    print('MD5加密input--${input}');
    var bytes = utf8.encode(input); // 将输入字符串编码为UTF-8字节数组
    var digest = md5.convert(bytes); // 计算MD5哈希值
    print("MQTT连接PASSWORD--${digest}");
    return digest.toString(); // 将哈希值转换为字符串并返回
  }

  static String currentTimestempMS() {
    DateTime now = DateTime.now();
    int milliseconds = now.millisecondsSinceEpoch;
    print("MQTT连接USERNAME--${milliseconds}");
    return milliseconds.toString();
  }

  /// 判断是否是手机号码
  static bool isPhone(String phone) {
    RegExp exp = RegExp(r"^1[23456789]\d{9}$");
    bool matched = exp.hasMatch(phone);
    return matched;
  }

  ///密码正则
  static bool isPsw(String password) {
    RegExp _pwdExp = RegExp(
        r'^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*#?&.])[A-Za-z\d@$!%*#?&.]{8,16}$');
    bool matched = _pwdExp.hasMatch(password);
    return matched;
  }
}

bool isNotEmpty(String? text) {
  return text?.isNotEmpty ?? false;
}

bool isEmpty(String? text) {
  return text?.isEmpty ?? true;
}
